<script>
var deviceCaller = TNM.Global.deviceCaller;
var globalActionConfig = IXW.Actions.configActions;

var showDialog = NV.Dialog.show;
var hideDialog = NV.Dialog.hide;
var confirmDialog = NV.Dialog.confirm;
var nvAlert = NV.Dialog.alert;

var deviceType = TCM.Const.DeviceTypes;
var deviceTypeNames = TCM.Const.DeviceTypeNames;
var getComboHTML = NV.Lib.getComboHTML;
var getNodeName = TCM.DeviceType.getNodeName;
</script>
<tpl id="editOther">
<ul class="{clz}">
	<li><span class="label">设备名称</span>
		<span><input class="readOnly" disabled="disabled" maxlength="30" id="device_name" value="{device_name}"></span></li>
	<li><span class="label">类型</span>{deviceTypeCombo}</li>
	<li><span class="label">厂家</span>
		<span><input class="readOnly" disabled="disabled" maxlength="30" id="device_provider" value="{device_provider}"></span></li>
	<li><span class="label">型号</span>
		<span><input class="readOnly" disabled="disabled" maxlength="30" id="device_style" value="{device_style}"></span></li>
	<li><span class="label">软件名称或网页地址</span>
		<span><input class="readOnly" disabled="disabled" maxlength="64" id="device_path" value="{device_path}"></span></li>
	<li class="pdu"><span class="label">用户名</span>
		<span><input class="readOnly" disabled="disabled" maxlength="15" id="device_username" value="{device_username}"></span></li>
	<li class="pdu"><span class="label">密码</span>
		<span><input class="readOnly" disabled="disabled" maxlength="15" id="device_password" value="{device_password}"></span></li>
	<li><span class="label">备注</span>
		<span><input class="readOnly" disabled="disabled" maxlength="150" id="device_desc" value="{device_desc}"></span></li>
	<li><span class="label">登录方式</span>
		<span><input class="loginIp" maxlength="15" id="device_loginIp" placeholder="IP" value="{device_loginIp}">
		<input class="loginPort" maxlength="5" id="device_loginPort" placeholder="PORT"  value="{device_loginPort}"></span></li>
</ul>
</tpl>
<tpl id="editDecoder">
<ul class="{clz}">
	<li><span class="label">名称</span>
		<span><input class="readOnly" disabled="disabled" maxlength="64" id="device_name" value="{device_name}"></span></li>
	<li><span class="label">IP地址</span>
		<span><input class="readOnly" disabled="disabled" maxlength="15" id="device_ip" value="{device_ip}"></span></li>
	<li><span class="label">用户名</span>
		<span><input class="readOnly" disabled="disabled" maxlength="15" id="device_username" value="{device_username}"></span></li>
	<li><span class="label">密码</span>
		<span><input class="readOnly" disabled="disabled" maxlength="15" id="device_password" value="{device_password}"></span></li>
	<li><span class="label">备注</span>
		<span><input class="readOnly" disabled="disabled" id="device_desc" value="{device_desc}"></span></li>
	<li><span class="label">登录方式</span>
		<span><input class="loginIp" maxlength="15" id="device_loginIp" placeholder="IP" value="{device_loginIp}">
		<input class="loginPort" maxlength="5" id="device_loginPort" placeholder="PORT" value="{device_loginPort}"></span></li>
</ul>
</tpl>

<tpl id="editSpecialDecoder">
<ul class="{clz}">
	<li><span class="label">名称</span>
		<span><input class="readOnly" disabled="disabled" maxlength="64" id="device_name" value="{device_name}"></span></li>
	<li><span class="label">IP地址</span>
		<span><input class="readOnly" disabled="disabled" maxlength="15" id="device_ip" value="{device_ip}"></span></li>
	<li><span class="label">用户名</span>
		<span><input class="readOnly" disabled="disabled" maxlength="15" id="device_username" value="{device_username}"></span></li>
	<li><span class="label">密码</span>
		<span><input class="readOnly" disabled="disabled" maxlength="15" id="device_password" value="{device_password}"></span></li>
	<li><span class="label">备注</span>
		<span><input class="readOnly" disabled="disabled" id="device_desc" value="{device_desc}"></span></li>
	<li><span class="label">登录方式</span>
		<span><input class="loginIp" maxlength="15" id="device_loginIp" placeholder="IP" value="{device_loginIp}">
		<input class="loginPort" maxlength="5" id="device_loginPort" placeholder="PORT" value="{device_loginPort}"></span></li>
</ul>
</tpl>

<tpl id="editIPC">
<ul class="{clz}">
	<li><span class="label">设备名称</span>
		<span><input class="readOnly" disabled="disabled" id="device_name" value="{device_name}"></span>
		<span class="label labelTwo">用户名</span>
		<span><input class="readOnly" disabled="disabled" id="device_username" value="{device_username}"></span></li>
	<li><span class="label">IP地址</span>
		<span><input class="readOnly" disabled="disabled" id="device_ip" value="{device_ip}"></span>
		<span class="label labelTwo">密码</span>
		<span><input class="readOnly" disabled="disabled" id="device_password" value="{device_password}"></span></li>
	<li><span class="label">端口号</span>
		<span><input class="readOnly" disabled="disabled" id="device_port" value="{device_port}"></span>
		<span class="label labelTwo">摄像机类型</span>{camerasTypeCombo}</li>
	<li><span class="label">组播流地址</span>
		<span><input class="readOnly" disabled="disabled" id="device_bcAddr" value="{device_bcAddr}"></span>
		<span class="label labelTwo">厂家</span>
		<span><input class="readOnly" disabled="disabled" id="device_provider" value="{device_provider}"></span></li>
	<li><span class="label">组播流端口</span>
		<span><input class="readOnly" disabled="disabled" id="device_bcPort" value="{device_bcPort}"></span>
		<span class="label labelTwo">型号</span>
		<span><input class="readOnly" disabled="disabled" id="device_style" value="{device_style}"></span></li>
	<li><span class="label">通道数</span>
		<span><input class="readOnly" disabled="disabled" id="device_channelNum" value="{device_channelNum}"></span>
		<span class="label labelTwo">所属分区</span>
		<span><input class="readOnly" disabled="disabled" id="device_zoneName" value="{device_zoneName}"></span></li>
	<li><span class="label">备注</span>
		<span><input class="readOnly" disabled="disabled" id="device_desc" value="{device_desc}"></span>
		<span class="label labelTwo">登录方式</span>
		<span><input class="loginIp" maxlength="15" id="device_loginIp" placeholder="IP" value="{device_loginIp}">
		<input class="loginPort" maxlength="5" id="device_loginPort" placeholder="PORT" value="{device_loginPort}"></span></li>
</ul>
</tpl>

<tpl id="editCoder">
<ul class="{clz} main">
	<li><span class="label">设备名称</span>
		<span><input class="readOnly" disabled="disabled" id="device_name" value="{device_name}"></span>
		<span class="label labelTwo">用户名</span>
		<span><input class="readOnly" disabled="disabled" id="device_username" value="{device_username}"></span></li>
	<li><span class="label">IP地址</span>
		<span><input class="readOnly" disabled="disabled" id="device_ip" value="{device_ip}"></span>
		<span class="label labelTwo">密码</span>
		<span><input class="readOnly" disabled="disabled" id="device_password" value="{device_password}"></span></li>
	<li><span class="label">端口号</span>
		<span><input class="readOnly" disabled="disabled" id="device_port" value="{device_port}"></span>
		<span class="label labelTwo">备注</span>
		<span><input class="readOnly" disabled="disabled" id="device_desc" value="{device_desc}"></span></li>
	<li><span class="label ">通道数</span>
		<span><input class="readOnly" disabled="disabled" id="device_channelNum" value="{device_channelNum}"></span>
		<span class="label labelTwo">登录方式</span>
		<span><input class="loginIp" maxlength="15" id="device_loginIp" placeholder="IP" value="{device_loginIp}">
		<input class="loginPort" maxlength="5" id="device_loginPort" placeholder="PORT" value="{device_loginPort}"></span>
		</li>
		<input id="device_type" value="{device_type}" style="display: none;">
	
</ul>
</tpl>

<script>
var	checkersBase = [
	{name : "name", key: "device_name", empty : "设备名称"},
	{name : "desc", key: "device_desc", empty : ""},
	{name : "username", key : "device_username", empty : ""},
	{name : "password", key : "device_password", empty : ""},
	{name : "loginIp", key : "device_loginIp", empty : ""},
	{name : "loginPort", key : "device_loginPort", empty : ""}
];
//dialog的配置项
var deviceDialogCfg = {
	decoder: {
		clz : "decoder-edit",
		tpl : t_editDecoder,
		checkers : checkersBase.concat([
			{name : "ip", key: "device_ip", empty : "设备IP"}
		])
	},
	specialDecoder: {
		clz : "decoder-edit",
		tpl : t_editSpecialDecoder,
		checkers : checkersBase.concat([
			{name : "ip", key: "device_ip", empty : "设备IP"}
		])
	},
	other: {
		clz : "other-edit",
		tpl : t_editOther,
		checkers : checkersBase.concat([
			{name : "provider", key : "device_provider", empty : "设备厂家"},
			{name : "style", key : "device_style", empty : "设备型号"},
			{name : "type", key: "device_type", empty : ""},
			{name : "path", key : "device_path", empty : "软件名称或网页地址"}
		])
	},
	coder: {
		clz : "coder-edit",
		tpl : t_editCoder,
		checkers : checkersBase.concat([
			{name : "ip", key: "device_ip", empty : "设备IP"},
			{name : "port", key: "device_port", empty : ""},
			{name : "channelNum", key: "device_channelNum", empty : ""},
			{name : "bc", key: "device_bc", empty : ""}
		])
	},
	IPC: {
		clz : "IPC-edit",
		tpl : t_editIPC,
		checkers : checkersBase.concat([
			{name : "provider", key : "device_provider", empty : "设备厂家"},
			{name : "style", key : "device_style", empty : "设备型号"},
			{name : "ip", key: "device_ip", empty : "设备IP"},
			{name : "bcAddr", key: "device_bcAddr", empty : "组播流地址"},
			{name : "bcPort", key: "device_bcPort", empty : "组播流端口"},
			{name : "channelNum", key: "device_channelNum", empty : ""},
			{name : "port", key: "device_port", empty : "端口号"},
			{name : "type", key: "camera_type", empty : ""},
			{name : "zoneName", key: "device_zoneName", empty : ""}
		])
	}
};
function checkIfIP(s){ return s.isIP();}
function checkIfPort(s){
	if (isNaN(s)) return false;
	var k = s - 0;
	if (k < 0 || k > 65535 || k !== parseInt(k)) return false;
	return s;
}
//验证表单是否可以提交
function verifyForm(value, msg, name){
	if(name == "loginIp"){
		if (!checkIfIP(value)){
			nvAlert("请输入正确的IP地址，例如：192.168.0.1");
			return false;
		}
	}
	if(name == "loginPort"){
		if (!checkIfPort(value)){
			nvAlert("请输入正确的端口号，端口号范围：0~65535");
			return false;
		}
	}
	return true;
}
//收集表单提交信息
function gatherInfo(rowModel, checkers, types){
	var data = {}, ifChanged = false;
	for (var i=0; i<checkers.length; i++){
		var checker = checkers[i];
		var val = null;
		if (checker.key == "device_bc")
			continue; 
		else
			val = $X(checker.key).value;
		if (!verifyForm(val, checker.empty,checker.name))
			return false;
		ifChanged = ifChanged || !(rowModel && rowModel.get(checker.name) == val);
		IX.setProperty(data, checker.name, val);
	}
	if (data.type == deviceType.Coder) {
		data.channelNum = data.channelNum-0;
		var bc = [];
		for (var k = 0; k < data.channelNum; k++) {
			var addr = $X("device_bcAddr"+(k+1)).value;
			var port = $X("device_bcPort"+(k+1)).value;
			bc.push({
				addr: addr,
				port: port-0,
				channelNo: k+1
			});
		}
		data.bc = bc;
	}
	if (!ifChanged){
		nvAlert("您未作任何修改，请点击取消关闭编辑窗口！");
		return false;
	}
	data.id = rowModel.getId();
	if(types.length === 1)
		data.type = types[0];
	return data;
}
//获取数据，显示dialog
function _editUnit(rowModel, cfg, okFn, types, siteId){
	var checkers = cfg.checkers;
	function _okFn(){
		var info = gatherInfo(rowModel, checkers, types);
		info.siteId=siteId;
		var checkFn = $XP(cfg, "checkInfo");
		if (!info || (IX.isFn(checkFn) && !checkFn(info))) return;
		deviceCaller(cfg.callerName, info, function(data){
			hideDialog();
			okFn({ids : [info.id === 0 ? info.id : (info.id || data.id)]});
		});
	}
	var data = {};
	for (var i=0; i<checkers.length; i++){
		var checker = checkers[i];
		data[checker.key] = rowModel && rowModel.get(checker.name) ? rowModel.get(checker.name).toString().dehtml(): "";
		if (checker.key == "device_bc")
			data[checker.key] = rowModel? rowModel.get(checker.name): "";
	}
	showDialog({
		clz : cfg.clz,
		title : cfg.title,
		content : cfg.tpl.renderData("", cfg.tpldataFn(rowModel, data)),
		listen : {ok : _okFn}
	});
}
//不可编辑的显示HTML
function getTransHTML(name,type){
	return "<span><input disabled id='"+name+ "' value='"+deviceTypeNames[type]+"'></span>";
}

IX.ns("TNM.DeviceDialog");
TNM.DeviceDialog.editDevice = function(rowModel, okFn, siteId){
	var types = [rowModel.get("type")];
	var key = getNodeName(types[0]);
	_editUnit(rowModel, IX.inherit(deviceDialogCfg[key], {
		title : "编辑设备",
		callerName : "editDevice",
		tpldataFn : function(rowModel, data){
			data.clz = data.device_type == 64 ? "PDU-edit" : "edit";
			data.deviceTypeCombo = getTransHTML("device_type",data.device_type || types[0]);
			data.camerasTypeCombo = getTransHTML("camera_type",data.camera_type);
			if (IX.Array.isFound(types[0], [20,21,22]))
				data.clz = "IPC-edit";
			data.channels = data.device_bc? data.device_bc : [{
				addr: "", 
				port: "", 
				channelNo: 1
			}];
			data.device_username = data.device_username || "admin";
			data.device_password = data.device_password || "admin";
			return data;
		}
	}), okFn, types, siteId);
};
TNM.DeviceDialog.logIn = function(rowModel, siteId){
	console.log("logIn");//TODO
};
</script>